package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private SoundPool mSoundPool = null;
    private HashMap<String, Integer> soundID = new HashMap<String, Integer>();
    private GridView gridView;
    private SimpleAdapter simpleAdapter;
    private List<Map<String, Object>> data_list;
    private int maxStreams = 12;
    String[] btnText = {"closehh", "crashl", "crashm", "crashr", "floor", "kick", "openhh", "ride", "snare", "tom1", "tom2", "tom3"};
    private Context context;

    private void buildMp3() {
        if (Build.VERSION.SDK_INT >= 21) {
            @SuppressLint("WrongConstant") Object localObject = new AudioAttributes.Builder().setLegacyStreamType(3).setContentType(4).setUsage(14).build();
            localObject = new SoundPool.Builder().setMaxStreams(maxStreams).setAudioAttributes((AudioAttributes) localObject).build();
            mSoundPool = (SoundPool) localObject;
        } else {
            mSoundPool = new SoundPool(maxStreams, AudioManager.STREAM_SYSTEM, 0);
        }

        mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int i, int i1) {
                /* 加载监听 */
            }
        });
        soundID.put(btnText[0], mSoundPool.load(this, R.raw.closehh, 1));
        soundID.put(btnText[1], mSoundPool.load(this, R.raw.crashl, 1));
        soundID.put(btnText[2], mSoundPool.load(this, R.raw.crashm, 1));
        soundID.put(btnText[3], mSoundPool.load(this, R.raw.crashr, 1));
        soundID.put(btnText[4], mSoundPool.load(this, R.raw.floor, 1));
        soundID.put(btnText[5], mSoundPool.load(this, R.raw.kick, 1));
        soundID.put(btnText[6], mSoundPool.load(this, R.raw.openhh, 1));
        soundID.put(btnText[7], mSoundPool.load(this, R.raw.ride, 1));
        soundID.put(btnText[8], mSoundPool.load(this, R.raw.snare, 1));
        soundID.put(btnText[9], mSoundPool.load(this, R.raw.tom1, 1));
        soundID.put(btnText[10], mSoundPool.load(this, R.raw.tom2, 1));
        soundID.put(btnText[11], mSoundPool.load(this, R.raw.tom3, 1));
    }

    private void play(String key) {
        mSoundPool.play(soundID.get(key), 1, 1, 0, 0, 1);
    }

    private void buildView() {
        data_list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < maxStreams; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("btnText", btnText[i]);
            data_list.add(map);
        }
        gridView = (GridView) findViewById(R.id.gridview);
        simpleAdapter = new SimpleAdapter(context, data_list, R.layout.button_item, new String[]{"btnText"}, new int[]{R.id.clickBtn});
        gridView.setAdapter(simpleAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Map<String, Object> map = (Map<String, Object>) adapterView.getItemAtPosition(i);
                play(map.get("btnText").toString());
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        buildMp3();
        buildView();
    }

}